
/* MUST SET APPROPRIATE FOLDER PATH WHERE THE CSV FILES ARE LOCATED*/
//note on terminology here: what is called a `match' in the paper (or supergame) is called a `round' //in this code, while what is called a `round' in the paper is called a `stage' in this code

cd "[PATH TO FOLDER HERE]"


clear
import delimited "BystanderCVS.csv"
rename ïsession session
bysort session period: egen Stage=total(stage)
bysort session period: egen Round=total(round)
drop stage round
egen ID=group(session subject)
set more off
gen session10pct=(session<=2|session>=9)

by ID, sort: gen nvals = _n ==1
gen Base=(player1choice==0) if type==1 
gen Deduct=(player1choice==2) if type==1
gen Transf=(player1choice==3) if type==1
gen No=(player2choice==0) if type==2
gen Yes=(player2choice==1) if type==2

save "Bystander_data.dta", replace


//how long did games go
bysort session group Round: egen EndStage=max(Stage)
count if EndStage==1 & session10pct==0& type==1
bysort session10pct: sum EndStage if Stage==1&type==1

//see outcomes per round (table in Appendix)
gen Outcome=.
replace Outcome=1 if Base==1 
replace Outcome=2 if Deduct==1
replace Outcome=3 if No==1
replace Outcome=4 if Yes==1
bysort session Round group Stage: egen GroupOutcome=max(Outcome)
gen SuccessfulTransfer75pct= (GroupOutcome==4) & session10pct==0 & type==1
gen FailedTransfer75pct= (GroupOutcome==3) & session10pct==0  & type==1
gen Baseline75pct= (GroupOutcome==1) & session10pct==0  & type==1
gen Deduct75pct = (GroupOutcome==2) & session10pct==0  & type==1

gen SuccessfulTransfer= (GroupOutcome==4)
gen session75pct=1-session10pct
bysort Stage: tab GroupOutcome if session10pct==0 & type==1 & Round>5
reg SuccessfulTransfer session75pct if type==1 & Round>5 & Stage==1

bysort session10pct: tab GroupOutcome if type==1 & Round>5 & Stage==1

//graphic of response to transfer requests
gen P2AfterTransf=(player2choice==0|player2choice==1)
gen player2choice10pct=player2choice if session10pct==1 & P2AfterTransf==1
gen player2choice75pct=player2choice if session10pct==0& P2AfterTransf==1
graph bar  player2choice10pct player2choice75pct  if Stage==1, over(Round) 




//stage 1 player 2
mlogit player2choice session10pct if type==2& Stage==1 & P2AfterTransf==1&Round>5,  pformat(%5.4f) base(0) vce(cluster ID)

margins, over(session10pct) vce(unconditional) pformat(%5.4f) post
margins,coeflegend
test _b[1bn._predict#1.session10pct]==1
test _b[2._predict#0bn.session10pct]==0
test _b[2._predict#1.session10pct]==0
test _b[2._predict#0bn.session10pct]=_b[2._predict#1.session10pct]
count if type==2& P2AfterTransf==1&Round>5 &Stage==1& session10pct==1
count if type==2& P2AfterTransf==1&Round>5 &Stage==1& session10pct==0

//any learning in transfer requests themselves?
gen late=.
replace late=0 if Round<=5
replace late=1 if Round>10
reg  Transf late if session10pct==0 & type==1 & Stage==1, cluster(ID)
reg  Transf late if session10pct==1 & type==1 & Stage==1, cluster(ID)


//average payoffs
bysort type session10pct: sum thisstagepayoff if Round>5 & Stage==1
bysort session Round Stage group: egen Surplus=total(thisstagepayoff)
bysort session10pct: sum Surplus

ranksum Surplus if Stage==1&Round>5&type==1,by(session10pct) exact




//player 1 choices 
mlogit player1choice session10pct if type==1& Round>5 & Stage==1, base(0) vce(cluster ID)  pformat(%5.4f)

margins, over(session10pct) vce(unconditional) pformat(%5.4f) post
margins,coeflegend
test (_b[1bn._predict#0bn.session10pct]=_b[1bn._predict#1.session10pct]) (_b[2._predict#0bn.session10pct]=_b[2._predict#1.session10pct]) ( _b[3._predict#0bn.session10pct]=_b[3._predict#1.session10pct]) 
test (_b[1bn._predict#0bn.session10pct]=_b[1bn._predict#1.session10pct])
test _b[3._predict#0bn.session10pct]=0.5
test _b[3._predict#1.session10pct]=0.5
test _b[3._predict#0bn.session10pct]=_b[3._predict#1.session10pct]
//baseline same?
test _b[1bn._predict#0bn.session10pct]=_b[1bn._predict#1.session10pct]
//deduct same?
test _b[2._predict#0bn.session10pct]=_b[2._predict#1.session10pct]
//transfer same?
test _b[3._predict#0bn.session10pct]=_b[3._predict#1.session10pct]



gen Stage1Outcome=.
replace Stage1Outcome=1 if Base==1 &Stage==1
replace Stage1Outcome=2 if Deduct==1&Stage==1
replace Stage1Outcome=3 if No==1&Stage==1
replace Stage1Outcome=4 if Yes==1&Stage==1
bysort session Round group: egen InitialOutcome=max(Stage1Outcome)
bysort group Round session: tab InitialOutcome
 
gen Category=.
replace Category=1 if InitialOutcome==1& session10pct==0
replace Category=2 if InitialOutcome==1& session10pct==1
replace Category=3 if InitialOutcome==2& session10pct==0
replace Category=4 if InitialOutcome==2& session10pct==1
replace Category=5 if InitialOutcome==3& session10pct==0
replace Category=6 if InitialOutcome==3& session10pct==1
replace Category=7 if InitialOutcome==4& session10pct==0
replace Category=8 if InitialOutcome==4& session10pct==1

count if Category==5& type==1&Round>5& Stage==2 
count if Category==6& type==1&Round>5& Stage==2 
count if Category==7& type==1&Round>5& Stage==2 
count if Category==8& type==1&Round>5& Stage==2 


//category>4 when there was a transfer request initially
mlogit player1choice i.Category if Round>5 & type==1 & Stage==2& Category>4& Category<8,base(0) vce(cluster ID) pformat(%5.4f)

margins, over(Category) vce(unconditional) pformat(%5.4f) post
margins,coeflegend
test _b[2._predict#5bn.Category]=_b[2._predict#6.Category]
 

 
 save "Bystander_data.dta", replace

 
 /* DEMOGRAPHICS */
 
 import delimited "surveyCVS.csv",clear
egen ID=group(session subject)
save "surveyCVS.dta",replace
use "Bystander_data.dta",clear
merge m:1 ID using "surveyCVS.dta"
drop _merge
save "Bystander_data.dta",replace
by ID, sort: gen indicator = _n == 1

 

gen Male=.
replace Male=1
replace Male=0 if strpos(gender,"fem" )| strpos(gender,"feam" )|  strpos(gender,"emale" )| strpos(gender,"W" )| strpos(gender,"w" )| gender=="F" | gender=="f" | strpos(gender,"FEMALE" )
replace Male=. if missing(gender)| strpos(gender,"on-binary")|strpos(gender,"not")|strpos(gender,"None")

//first stage by gender
mlogit player1choice Male#session10pct if type==1& Stage==1& Round>5,  pformat(%5.4f) vce(cluster ID) base(0)




margins, over(Male) at (session10pct=(0 1))  pformat(%5.4f) vce(unconditional) post
margins,coeflegend

tab Male session10pct if type==1 & Stage==1 & Round>5

//do women and men act same in 10 percent treatment
test  (_b[3._predict#2._at#0bn.Male]= _b[3._predict#2._at#1.Male]) (_b[2._predict#2._at#0bn.Male]=_b[2._predict#2._at#1.Male])

//do women and men act same in 75 percent treatment
test  (_b[3._predict#1bn._at#0bn.Male]= _b[3._predict#1bn._at#1.Male]) (_b[2._predict#1bn._at#0bn.Male]=_b[2._predict#1bn._at#1.Male])


//second stage
mlogit player1choice Male if type==1& Stage==2& Round>5&Category==5,  pformat(%5.4f) vce(cluster ID) base(0)

margins, over(Male) vce(unconditional) pformat(%5.4f)  post
margins,coeflegend
test (_b[1bn._predict#0bn.Male]= _b[1bn._predict#1.Male]) (_b[2._predict#0bn.Male]= _b[2._predict#1.Male])  (_b[3._predict#0bn.Male]= _b[3._predict#1.Male]) 
tab Male if type==1& Stage==2& Round>5&Category==5

 tab Male session10pct if Stage==1& Round==1 
  tab Male session10pct if Stage==1& Round==1 & type!=3,column
 
//similar following yes from player 2, not shown in table
mlogit player1choice Male if type==1& Stage==2& Round>5& Category==7,  pformat(%5.4f) vce(cluster ID) base(0)
margins, over(Male)  vce(unconditional) post
margins,coeflegend
test (_b[1bn._predict#0bn.Male]= _b[1bn._predict#1.Male]) (_b[2._predict#0bn.Male]= _b[2._predict#1.Male]) 
tab Male if type==1& Stage==2& Round>5&Category==7


//similar as bystanders
logit player2choice i.Male#i.session10pct if type==2 & P2AfterTransf==1&Stage==1 & Round>5, vce(cluster ID) pformat(%5.4f)
margins, over(Male) at (session10pct=(0 1)) vce(unconditional)  post
margins,coeflegend
//no real diff in 10%
test  _b[2._at#1.Male]=_b[2._at#0bn.Male]
//no real diff in 10%
test _b[1bn._at#1.Male]=_b[1bn._at#0bn.Male]


egen concentration_index_aux=group(concentration)
gen concentration_index=5
replace concentration_index=1 if inlist(concentration_index_aux, 1,7,9,13,16,19,36,37,38,39,40,44,55,56,57,60,75,76,85,106,111,112,117,118,119,125,129) //Physical Sciences
replace concentration_index=2 if inlist(concentration_index_aux,2,3,5,6,8,10,11,12,14,15,17,18,20,21,22,23,45,46,47,48,49,50,51,52,53,70,71,86,87,88,90,91,92,93,94,100,102,113,114,120,123,126,130,131,132,133,134) //Social Sciences
replace concentration_index=3 if inlist(concentration_index_aux,34,35,42,54,58,59,61,65,66,67,68,72,73,74,79,80,81,82,96,101,115,116,122,124,127) // Humanities
replace concentration_index=4 if inlist(concentration_index_aux, 24,25,26,27,28,29,30,31,32,33,43,62,63,64,69,77,78,83,84,89,103,104,105,107,108,109,110,121,128) //Social Sci
label define concentration 1 "Physical Sciences" 2 "Social Sciences" 3 "Humanities" 4 "Life Sciences" 5 "NS",replace
label values concentration_index concentration
tab concentration_index if indicator==1
